package com.hbgc.controller;

import com.github.pagehelper.PageInfo;
import com.hbgc.pojo.TbType;
import com.hbgc.pojo.TbVideo;
import com.hbgc.service.IVideoService;
import com.hbgc.service.IVideoTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

@Controller
public class HomeController {

    @Autowired
    private IVideoService iVideoService;

    @Autowired
    private IVideoTypeService iVideoTypeService;

    @RequestMapping("/")
    public ModelAndView index()
    {
        Map tbVideoByIndex = iVideoService.getTbVideoByIndex();
        ModelAndView mv=new ModelAndView();
        mv.addObject("tbVideoByIndex",tbVideoByIndex);
        mv.setViewName("/home/index");
        return mv;
    }


    @RequestMapping("/list/{videotype}")
    public  ModelAndView list(@RequestParam(required = false,defaultValue = "1") int page,@PathVariable  Long videotype,@RequestParam Map search)
    {
        String url="&";
        System.out.println(search);
        search.remove("page");
        Set<Map.Entry> set = search.entrySet();

        for(Map.Entry entry:set)
        {
            url+=entry.getKey()+"="+entry.getValue()+"&";
        }
        TbType tbType = iVideoTypeService.getTbTypeById(videotype);
        ModelAndView mv=new ModelAndView();
        PageInfo pageInfo = iVideoService.getTbVideoByTypePid(page, 20, videotype.intValue(),search);
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("typeid",videotype);
        mv.addObject("type",tbType);
        mv.addObject("search",search);
        mv.addObject("url",url);
        mv.setViewName("/home/list");
        return mv;
    }


    @RequestMapping("/search")
    public  ModelAndView search(@RequestParam(required = false,defaultValue = "1") int page,@RequestParam String keyword)
    {
        ModelAndView mv=new ModelAndView();
        Map searchMap=new LinkedHashMap();
        searchMap.put("videoname",keyword);
        PageInfo pageInfo = iVideoService.queryTbVideoByPage(page, 20, searchMap);
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("keyword",keyword);
        mv.setViewName("/home/search");

        return mv;
    }


    @RequestMapping("/detail/{videoid}")
    public  ModelAndView info(@PathVariable long videoid)
    {
        TbVideo video = iVideoService.getTbVideoById(videoid);
        TbType type=iVideoTypeService.getTbTypeById(new Long(video.getTypeid()));
        String playurl = video.getPlayurl();
        String[] urls=playurl.split("\r\n");
        ModelAndView mv=new ModelAndView();
        mv.addObject("video",video);
        mv.addObject("type",type);
        mv.addObject("urls",urls);
        mv.setViewName("/home/info");
        return mv;
    }



    @RequestMapping("/play/{videoid}/{videonum}")
    public ModelAndView play(@PathVariable long videoid, @PathVariable int videonum)
    {
        ModelAndView mv=new ModelAndView();
        TbVideo video = iVideoService.getTbVideoById(videoid);
        TbType type=iVideoTypeService.getTbTypeById(new Long(video.getTypeid()));
        String playurl = video.getPlayurl();
        String[] urls=playurl.split("\r\n");
        String url=urls[videonum];
        mv.addObject("video",video);
        mv.addObject("type",type);
        mv.addObject("urls",urls);
        mv.addObject("url",url);
        mv.setViewName("/home/play");
        return mv;
    }

}
